package com.ruoyi.plus.utils.sms.api;

import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.version.ApiVersion;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.plus.utils.sms.ISmsService;
import com.ruoyi.plus.utils.sms.domain.CheckCodeModel;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @author liqingsong
 */
@Slf4j
@Api(tags = "SmsApi-短信")
@ApiVersion
@RestController
@RequestMapping("/app/api/plus/SmsApi")
public class SmsApi extends BaseApi {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISmsService smsService;

    @ApiOperation("sendSms-发送验证码")
    @GetMapping("/sendSms")
    @ResponseBody
    public ApiResult<Boolean> sendSms(@RequestParam(value = "phoneNumber") String phoneNumber) throws Exception {
        if (!validatePhone(phoneNumber)) {
            return new ApiResult<Boolean>(this.getApiVersion()).setCode(-1).setData(false).setMsg("手机号格式错误！");
        }
        String code = smsService.genCode(phoneNumber);
        Future<Boolean> futureResult = smsService.sendCode(phoneNumber, code);
        Boolean result = futureResult.get(10, TimeUnit.SECONDS);
        if (result != null && !result) {
            return new ApiResult<Boolean>(this.getApiVersion()).setData(false).setMsg("验证码发送失败,请稍后再试！");
        }
        return new ApiResult<Boolean>(this.getApiVersion()).setData(true).setMsg("验证码发送成功！");
    }

    @ApiOperation("sendSmsCheckPhone-校验用户发送验证码")
    @GetMapping("/sendSmsCheckPhone")
    @ResponseBody
    public ApiResult<Boolean> sendSmsCheckPhone(@RequestParam(value = "phoneNumber") String phoneNumber) throws Exception {
        if (!validatePhone(phoneNumber)) {
            return new ApiResult<Boolean>(this.getApiVersion()).setCode(-1).setData(false).setMsg("手机号格式错误！");
        }
        //校验用户
        SysUser sysUser = sysUserService.selectUserByPhoneNumber(phoneNumber);
        if (sysUser == null) {
            return new ApiResult<Boolean>(this.getApiVersion()).setData(false).setCode(-1).setMsg("手机号未注册!");
        }
        String code = smsService.genCode(phoneNumber);
        Future<Boolean> futureResult = smsService.sendCode(phoneNumber, code);
        Boolean result = futureResult.get(10, TimeUnit.SECONDS);
        if (result != null && !result) {
            return new ApiResult<Boolean>(this.getApiVersion()).setData(false).setMsg("验证码发送失败,请稍后再试！");
        }
        return new ApiResult<Boolean>(this.getApiVersion()).setData(true).setMsg("验证码发送成功！");
    }

    @ApiOperation("checkSms-验证验证码")
    @GetMapping("/checkSms")
    @ResponseBody
    public ApiResult<Boolean> checkSms(@RequestParam(value = "phoneNumber") String phoneNumber, @RequestParam(value = "code") String code) {
        if (!validatePhone(phoneNumber)) {
            return new ApiResult<Boolean>(this.getApiVersion()).setCode(-1).setData(false).setMsg("手机号格式错误！");
        }
        CheckCodeModel checkCodeModel = smsService.checkCode(phoneNumber, code);
        if (!checkCodeModel.isCheckResult()) {
            return new ApiResult<Boolean>(this.getApiVersion()).setData(false).setCode(-1).setMsg(checkCodeModel.getCheckMsg());
        }
        return new ApiResult<Boolean>(this.getApiVersion()).setData(true).setMsg("验证码校验成功");
    }

    /**
     * 正则：手机号（简单）, 1字头＋10位数字即可.
     *
     * @param phoneNumber 手机号
     * @return 是否是手机号
     */
    public static boolean validatePhone(String phoneNumber) {
        if (StringUtils.isNotEmpty(phoneNumber)) {
            Pattern pattern = Pattern.compile("^[1]\\d{10}$");
            return pattern.matcher(phoneNumber.trim()).matches();
        }
        return false;
    }

}
